#include "AnodWeather.hpp"
#include <QPainter>
#include <QFontMetrics>
#include <QSizeF>
#include <QDomDocument>
#include <QFile>
#include <QTextStream>

#include <KIcon>
#include <KIconLoader>
 
#include <plasma/svg.h>
#include <plasma/theme.h>
#include <plasma/tooltipmanager.h>
#include <plasma/tooltipcontent.h>

#include <iostream>

// 导出部件接口
K_EXPORT_PLASMA_APPLET(WeatherWidget, AnodWeather)

class AnodWeatherImpl
{
public:
  AnodWeatherImpl()
    :the_tip( new Plasma::ToolTipContent() )
  {
  }

  ~AnodWeatherImpl()
  {
    delete the_tip;
  }

  Plasma::ToolTipContent& getTip()
  {
    return *the_tip;
  }

  void updateTip()
  {
    QDomDocument doc;
    QFile file( "stdout.txt" );
    if( !file.open( QIODevice::ReadOnly ) )
      {
	exit(1);
      }

    QTextStream xml( &file );
    xml.setCodec( "UTF-8" );
    QString xml_file = xml.readAll();

    if( !doc.setContent( xml_file ) )
      {
	exit( 2 );
      }
    file.close();

    QDomElement elem = doc.documentElement();
    elem = elem.firstChildElement( "ToolTipContent" );
    if( !elem.isNull() )
      {
	QDomElement tooltip_info = elem.firstChildElement();
	while( !tooltip_info.isNull() )
	  {
	    if( tooltip_info.tagName() == "MainText" )
	      {
		getTip().setMainText( tooltip_info.text() );
	      }
	    else if( tooltip_info.tagName() == "SubText" )
	      {
		getTip().setSubText( tooltip_info.text() );
	      }
	    else
	      {
	      }

	    tooltip_info = tooltip_info.nextSiblingElement();
	  }
      }
  }

private:
  Plasma::ToolTipContent  *the_tip;
};
 
AnodWeather::AnodWeather(QObject *parent, const QVariantList &args)
: Plasma::Applet(parent, args),
  _label(this),
  _pen_color(Qt::white),
  _p_impl( new AnodWeatherImpl() )
{
  _label.setText("Anod's Weather Widget");
  _label.setAlignment( Qt::AlignCenter );

  setBackgroundHints(DefaultBackground);
  resize(200, 200);
}
 
 
AnodWeather::~AnodWeather()
{
  if (hasFailedToLaunch()) {
    // Do some cleanup here
  } else {
    // Save settings
  }
}
 
void AnodWeather::init()
{
  _p_impl->updateTip();
  Plasma::ToolTipManager::self()->setContent( this, _p_impl->getTip() );
  // A small demonstration of the setFailedToLaunch function
  /*  if (m_icon.isNull()) {
    setFailedToLaunch(true, "No world to say hello");
    }*/
} 

static const char *color_str;

void AnodWeather::paintInterface(QPainter *p,
				     const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
{
  //  std::cerr << "paintInterface" << std::endl
  //            << contentsRect.width() << ":" << contentsRect.height() << std::endl;
  p->setRenderHint(QPainter::Antialiasing);

  _label.resize( 146, 20 );
  _label.setPos( contentsRect.width()/2-_label.boundingRect().width()/2, 
		  contentsRect.height()/2-_label.boundingRect().height()/2 );
  //  _label.adjustSize();

  p->setPen( _pen_color );
  p->drawEllipse( contentsRect.x(), contentsRect.y(),  contentsRect.width(), contentsRect.height() );
  
  /*  p->setPen(Qt::white);
  p->drawText(contentsRect,
	      Qt::AlignCenter,
	      "Anod Widget!");
  */
}
 
#include "AnodWeather.moc"
